Consider the following c-program#include <stdio.h>int r()...
for (r();r();r())
for initialization r() function will execute num will become 6
for conditional checking r() will execute and num will be 5
body of the loop executed and return value will be 5 and hence 5 will be printed
increment part of the loop will be executed as r() and value will be 3
again condition will be tested as r() num will be 2
body of the loop will print as r(), 2 will be printed value decremented to 1
loop will break
final value printed is 52
View all questions of this test
Consider the following c-program#include <stdio.h>int r()...
First, let's analyze the given code:
- **r() function**: This function is a static function that returns the value of a static variable `num` decremented by 1 each time it is called.
- **main() function**: In the `main()` function, a `for` loop is used to call the `r()` function three times. The loop condition `r()` will return 7, 6, and 5 respectively in the first three iterations.
- **printf statement**: Inside the loop, the `printf()` statement will print the value returned by `r()` after decrementing it by 1. So, the output will be 6, 5, and 4 respectively.
Now, let's trace the values returned by the `r()` function during the execution of the program:
1. The first call to `r()` in the `for` loop will return 7, but it will not be printed.
2. The second call to `r()` in the loop condition will return 6, and it will be printed.
3. The third call to `r()` in the loop condition will return 5, and it will be printed.
4. The fourth call to `r()` in the loop condition will return 4, and it will be printed.
Therefore, the values displayed on execution of the program will be **52**.